package com.example.demo.service.Impl;

import com.example.demo.domain.Goods;
import com.example.demo.domain.IndexGoods;
import com.example.demo.domain.Picture;
import com.example.demo.repository.GoodsMapper;
import com.example.demo.repository.PictureMapper;
import com.example.demo.service.GoodsService;
import com.example.demo.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Resource
    private PictureMapper pictureMapper;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Goods getGoodsById(Integer id) {
        return goodsMapper.getGoodsById(id);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Goods> getAll() {
        return goodsMapper.getAll();
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Goods> getSearch(String searchText) {
        return goodsMapper.getSearch(searchText);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Goods> getByRange(Integer from, Integer pageNum) {
        return goodsMapper.getByRange(from,pageNum);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void delGoodsChangeStateById(Integer id)
    {
        goodsMapper.delGoodsChangeStateById(id);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Goods getBaseGoodsbyId(Integer id) {
        return goodsMapper.getBaseGoodsbyId(id);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void addGoods(Goods goods,List<String> picURL) {
        String time=TimeUtils.getStringDate();
        goods.setTimes(time);
        goodsMapper.addGoods(goods);
        for(String s:picURL)
        {
            Picture picture=new Picture();
            picture.setPicURl(s);
            picture.setTimes(time);
            picture.setGid(goodsMapper.getIdByName(goods.getName()));
            pictureMapper.addPicture(picture);
        }

    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void delGoodsById(Integer id) {
        goodsMapper.delGoodsById(id);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void saveGoods(Goods goods,List<String> picURL) {
        goodsMapper.saveGoods(goods);
        for(String s:picURL)
        {
            Picture picture=new Picture();
            picture.setPicURl(s);
            picture.setTimes(TimeUtils.getStringDate());
            picture.setGid(goods.getId());
            pictureMapper.addPicture(picture);
        }

    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Integer getIdByName(String name) {
        return goodsMapper.getIdByName(name);
    }
}
